﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;

using Willson.JsbManager.Bll;
using Willson.JsbManager.Comm;
using Willson.JsbManager.BllEntity;

namespace Willson.JsbManager.UI
{
    /// <summary>
    /// 月用水信息录入窗口
    /// </summary>
    public partial class XTCompanySbMonthInputForm : BaseForm
    {
        private int m_year = 0;
        private int m_month = 0;
        private BindingList<BESbMonthInput> list = null;
        
        public XTCompanySbMonthInputForm()
        {
            InitializeComponent();

            dataGridView.RowHeadersWidth = 20;
            dataGridView.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            dataGridView.AllowUserToResizeRows = false;
            dataGridView.RowHeadersWidth = 20;
            dataGridView.AutoGenerateColumns = false;
   
        }
        
        public override void SelectYearMonth(int year, int month)
        {
            if (year == 0 || month == 0)
                this.Close();

            FunctionName.Text = string.Format("{0}年{1}月{2}", year, month, FunctionName.Text);

            this.m_year = year;
            this.m_month = month;

            list = BLLFactory<BLLSbMonthInput>.Instance.GetListInfo(year, month);

            dataGridView.DataSource = list;
        }

        private void XTCompanySbMonth_Shown(object sender, EventArgs e)
        {
            SelectYearMonthForm form = new SelectYearMonthForm(this);
            form.ShowDialog();
        }

        private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            int result = 0;
            
            if (this.dataGridView.Columns[e.ColumnIndex].Name == "sb_cjs")
            {
                if (string.IsNullOrEmpty(e.FormattedValue.ToString())) return;
                if (!int.TryParse(e.FormattedValue.ToString(), out result))
                {
                    e.Cancel = true;
                }
            }

            if (this.dataGridView.Columns[e.ColumnIndex].Name == "sb_ysl")
            {
                if (string.IsNullOrEmpty(e.FormattedValue.ToString())) return;
                if (!int.TryParse(e.FormattedValue.ToString(), out result))
                {
                    e.Cancel = true;
                }
            }
        }

        private void bt_company_hb_Click(object sender, EventArgs e)
        {
            if (this.dataGridView.RowCount == 0) return;

            ComputerHbForm from = new ComputerHbForm(this);
            from.ShowDialog();
        }

        /// <summary>
        /// 输入换表度数
        /// </summary>
        /// <param name="num"></param>
        public void InputHbNumber(int num)
        {
      
            this.dataGridView.Rows[dataGridView.CurrentCell.RowIndex].Cells["sb_hb"].Value = num;

        }

        private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            int begin = 0;
            int hb = 0;
            int ysl = 0;
            int cjs = 0;
            
            if (dataGridView.Columns[e.ColumnIndex].Name == "sb_cjs")
            {
                begin = Convert.ToInt32(dataGridView.Rows[e.RowIndex].Cells["sb_begin"].Value);
                hb = Convert.ToInt32(dataGridView.Rows[e.RowIndex].Cells["sb_hb"].Value);
                cjs = Convert.ToInt32(dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);

                if (cjs == 0) return;

                if (hb > 0)
                {
                    ysl = hb - begin + cjs;
                }
                else
                {
                    ysl = cjs - begin;
                }

                this.dataGridView.Rows[e.RowIndex].Cells["sb_ysl"].Value = ysl;
            }
        }

        private void bt_close_Click(object sender, EventArgs e)
        {
            if (MessageUtil.ShowYesNoAndWarning("真的要关闭吗？") == DialogResult.Yes)
                Close();
        }

        private void bt_save_Click(object sender, EventArgs e)
        {
            int li_id = 0;
            int li_sb_id = 0;
            int li_hb = 0;
            int li_cjs = 0;
            int li_ysl = 0;
            string ls_memo = string.Empty;
            
            if (this.dataGridView.RowCount == 0) return;

            if (MessageUtil.ShowYesNoAndWarning("真的要保存吗？") == DialogResult.No)
                return;

            for (int index = dataGridView.RowCount - 1; index >= 0; index--)
            {
                li_id = Convert.ToInt32(dataGridView.Rows[index].Cells["sb_month_id"].Value);
                li_sb_id = Convert.ToInt32(dataGridView.Rows[index].Cells["sb_id"].Value);
                li_hb = Convert.ToInt32(dataGridView.Rows[index].Cells["sb_hb"].Value);
                li_cjs = Convert.ToInt32(dataGridView.Rows[index].Cells["sb_cjs"].Value);
                li_ysl = Convert.ToInt32(dataGridView.Rows[index].Cells["sb_ysl"].Value);
                if (dataGridView.Rows[index].Cells["memo"].Value != null)
                    ls_memo = dataGridView.Rows[index].Cells["memo"].Value.ToString();

                if (li_cjs == 0 && li_hb == 0)
                    continue;

                BLLFactory<BLLSbMonthInput>.Instance.Save(m_year,m_month,li_sb_id,li_id, li_hb, li_cjs, li_ysl, ls_memo);

                foreach (BESbMonthInput obj in list)
                {
                    if (obj.Sb_month_id == li_id)
                    {
                        bool a = list.Remove(obj);
                        break;
                    }
                }
               
            }

        }

        private void bt_print_Click(object sender, EventArgs e)
        {
            BindingList<BESbMonthInput> list = BLLFactory<BLLSbMonthInput>.Instance.GetListInfo(m_year, m_month);

            IEnumerator<BESbMonthInput> ator = list.GetEnumerator();

            PrintDataSet ds = new PrintDataSet();

            while (ator.MoveNext())
            {
                DataRow objRow;

                objRow = ds.Tables["sb_month_print"].NewRow();
                objRow["user_code"] = ator.Current.User_code;
                objRow["ht_code"] = ator.Current.Ht_code;
                objRow["company_name"] = ator.Current.Company_name;
                objRow["sb_begin"] = ator.Current.Sb_begin;
                objRow["sb_cjs"] = "";
                objRow["sb_ysl"] = "";

                ds.Tables["sb_month_print"].Rows.Add(objRow);

            }

            List<ReportParameter> listPara = new List<ReportParameter>();

            ReportParameter para = new ReportParameter("Top_text",string.Format("{0}年{1}月{2}", m_year, m_month, "用水信息录入表"));

            listPara.Add(para);

            XTPrintViewForm form = new XTPrintViewForm();
            form.Bind(ds, "sb_month_print", "Willson.JsbManager.UI.SbMonthInput.rdlc", "PrintDataSet_sb_month_print", listPara);
            form.ShowDialog();
          
        }

    }
}
